/*
 * @(#)CosineSimilarity.java	1.0 01/01/05
 *
 * Copyright 2007 Fabio Gasparetti. All rights reserved.
 */
package org.tabularium.text;

/**
 * Draws text similarities according to the cosine measure.
 * 
 * @author  Fabio Gasparetti
 * @version 1.0, 01/01/05
 */
public class CosineSimilarity implements TextSimilarity {

	public CosineSimilarity() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tabularium.text.TextSimilarity#similarity(java.lang.String,
	 *      java.lang.String)
	 */
	public double similarity(String text1, String text2) {
		text1 = text1.toLowerCase();
		text2 = text2.toLowerCase();
		TermWeightVector v1 = TermWeightVector
				.build(TermWeightVector.tokenize(text1));
		TermWeightVector v2 = TermWeightVector
				.build(TermWeightVector.tokenize(text2));
		return this.similarity(v1, v2);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tabularium.text.TextSimilarity#similarity(org.tabularium.text.TermFreqVector,
	 *      org.tabularium.text.TermFreqVector)
	 */
	public double similarity(TermWeightVector v1, TermWeightVector v2) {
		return v1.cosineRule(v2);
	}
}
